<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters" class="demo-form-inline">
        <el-form-item label="商品名称">
          <el-input v-model="filters.keyword" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryBaran">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="goods" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="id" label="商品ID" width="160" sortable>
      </el-table-column>
      <el-table-column prop="sellerId" label="卖家" width="140" sortable>
      </el-table-column>
      <el-table-column prop="goodsName" label="商品名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="caption" label="副标签" width="160" sortable>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="160" sortable>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审核" width="100" sortable>
        <template scope="scope">
          <span v-if="scope.row.auditStatus==='0'">未申请</span>
          <span v-if="scope.row.auditStatus==='1'">申请中</span>
          <span v-if="scope.row.auditStatus==='2'">审核通过</span>
          <span v-if="scope.row.auditStatus==='3'">已驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button type="primary" size="small" @click="show(scope.row)">详情</el-button>
          <el-button type="success" size="small" @click="submit(scope.row,1)">审核通过</el-button>
          <el-button type="danger" size="small" @click="submit(scope.row,0)">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length===0" >批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="pageNum" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="showGoods" label-width="80px"  ref="editForm">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="showGoods.goodsName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="showGoods.price" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品介绍" prop="introduction">
          <quill-editor v-model="showGoods.introduction" auto-complete="off"></quill-editor>
        </el-form-item>
        <el-form-item label="商品包装" prop="packageList">
          <quill-editor v-model="showGoods.packageList" auto-complete="off"></quill-editor>
        </el-form-item>
        <el-form-item label="售后服务" prop="saleService">
          <quill-editor v-model="showGoods.saleService" auto-complete="off"></quill-editor>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-cascader
              :options="tree" style="width: 500px" v-model="categoryId" @change="changeNode"
              :props="{ checkStrictly: true,
                      label:'label',
                      value:'value' }"
              clearable></el-cascader>
        </el-form-item>
        <el-form-item label="商品品牌">
          <el-select v-model="showGoods.brandId" clearable placeholder="请选择">
            <el-option
                v-for="item in brands"
                :key="item.id"
                :label="item.text"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
              v-model="showGoods.itemImages"
              class="upload-demo"
              action="http://localhost:10010/file/upload"
              :on-success="success"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="审核状态" prop="firstChar">
          <span v-if="showGoods.auditStatus==='0'">未申请</span>
          <span v-if="showGoods.auditStatus==='1'">申请中</span>
          <span v-if="showGoods.auditStatus==='2'">审核通过</span>
          <span v-if="showGoods.auditStatus==='3'">已驳回</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">确定</el-button>
<!--        <el-button type="primary" @click.native="editSubmit">提交</el-button>-->
      </div>
    </el-dialog>
  </section>
</template>

<script>
import axios from "axios";
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
/*引入富文本编辑器集成文件上传服务器控件*/
import quillConfig from '@/assets/quill-config.js'
export default {
  components: {
    quillEditor
  },
  data() {
    return {
      goods: [],
      fileList: [
        {
          name: '',
          url: ''
        }
      ],
      tree: [],
      ids: [],
      title: '',
      filters: {
        keyword: '',
        firstChar: ''
      },
      categoryId: [],
      brands: [],
      showGoods: {
        auditStatus: '',
        brandId: null,
        category1Id:null,
        category2Id:null,
        category3Id:null,
        price:null,
        goodsName: '',
        introduction: '',
        packageList: '',
        saleService: '',
        typeTemplateId: '',
        itemImages:''
      },
      total: 0,
      pageNum: 1,
      pageSize:10,
      listLoading: false,
      sels: [],//列表选中列
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,

    }
  },
  methods: {
    submit(row,index) {
      if (index == 1) {
        row.auditStatus = "2";
      } else {
        row.auditStatus = "3";
      }
      axios.post("/goods/updateStatus",row).then(resp=>{
        console.log(resp);
      }).catch(resp=>{

      })
    },
    success(response,file,fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      this.showGoods.itemImages=response.data;
    },
    //获取商品列表
    queryBaran(){
      // 准备参数
      let params = {
        pageSize: this.pageSize, //页面大小
        pageNum: this.pageNum,   //页码
        trimParam:{
          name:this.filters.keyword,
          firstChar: this.filters.firstChar
        }
      }
      this.$http.post("/goods/queryPageByAdmin",params)
          .then(resp=>{
            // console.log(resp);
            this.goods = resp.data.data.list;
            this.total = resp.data.data.total;
          }).catch(resp=>{
        alert("网络异常，稍后重试");
      });
      //查询树结构
      axios.get("/itemcat/queryTree").then(resp => {
        // console.log(resp);
        this.tree = resp.data.data;
      }).catch(resp => {

      });

    },
    //页面加载查询品牌
    queryBrands() {
      console.log(this.categoryId);
      var iterId = this.categoryId[this.categoryId.length - 1];
      console.log(iterId);
      var temp = [];
      axios.get("itemcat/queryBrandById/"+iterId).then(resp=>{
        // console.log(resp);
        this.brands = JSON.parse(resp.data.data.brandIds);
        console.log(this.brands);
        // this.customAttributes = JSON.parse(resp.data.data.customAttributeItems);
        // this.specs = JSON.parse(resp.data.data.specIds);
        // console.log(this.specs);
        // for (let i = 0; i < this.specs.length; i++) {
        //   temp.push(this.specs[i].id);
        // }
        // console.log(temp);
      }).catch(resp=>{

      });
    },
    //删除
    handleDel: function (index, row) {
      console.log(row);
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/brand/deleteById/"+row.id)
            .then(resp=>{
                  //去除忙等框
                  if (resp.data.code==="1") {
                    this.editFormVisible = false;
                    this.$message({
                      message: '删除成功',
                      type: 'success'
                    });
                    this.listLoading = false;
                    this.queryBaran();
                  }
                }

            )
            .catch(result=>{
              //去除忙等框
              this.listLoading = false;
              //提示
              this.$message({
                message: '系统错误！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    },
    //模态框选择节点
    changeNode(value) {
      // console.log(value);
      this.categoryId = value;
      this.showGoods.category1Id=this.categoryId[0];
      this.showGoods.category2Id=this.categoryId[1];
      this.showGoods.category3Id=this.categoryId[2];
    },
    //显示编辑界面
    show (row) {
      // console.log(row);
      this.editFormVisible = true;
      this.title = "商品详情";
      var temp = [];
      if (row.category1Id != null) {
        temp.push(row.category1Id);
      }
      if (row.category2Id != null) {
        temp.push(row.category2Id);
      }
      if (row.category2Id != null) {
        temp.push(row.category3Id);
      }
      this.categoryId = temp;
      this.showGoods = row;
      this.fileList[0].url = row.itemImages;
      this.fileList[0].name = row.itemImages;
      this.queryBrands();
    },
    //编辑
    editSubmit: function () {

      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            console.log(this.showGoods);
            //NProgress.start();
            axios.post("/goods/updateInfo",this.showGoods).then(resp=>{
              console.log(resp);
              if (resp.data.code==="1") {
                this.editFormVisible = false;
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                this.editLoading = false;
                this.queryBaran();
              }
            }).catch(resp=>{

            })
          });
        }
      });
    },
    //新增
    // addSubmit: function () {
    //   this.$refs.addForm.validate((valid) => {
    //     if (valid) {
    //       this.$confirm('确认提交吗？', '提示', {}).then(() => {
    //         this.addLoading = true;
    //         //NProgress.start();
    //         let para = Object.assign({}, this.addForm);
    //         para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
    //         addDepartment(para).then((res) => {
    //           this.addLoading = false;
    //           //NProgress.done();
    //           this.$message({
    //             message: '提交成功',
    //             type: 'success'
    //           });
    //           this.$refs['addForm'].resetFields();
    //           this.addFormVisible = false;
    //           this.getDepartments();
    //         });
    //       });
    //     }
    //   });
    // },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.queryBaran();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    selsChange: function (sels) {
      // console.log(sels);
      this.sels = sels;
    },
  },
  mounted() {
    this.queryBaran();
  }
}

</script>

<style scoped>

</style>